﻿#region Using Statemens
using System;
using System.Collections.Generic;
using GameBase.Screens;
using GameBase.Sprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StarFox.ScreenComponents;
#endregion

namespace StarFox.Screens
{
    /// <summary>
    /// Main menu screen of the game.
    /// </summary>
    public class MainMenuScreen : StarFoxScreen
    {
        #region Fields

        GameScore gameScore;

        bool toPlay;

        #endregion

        #region Screen Transitions

        bool toHidden = false;

        float trTime = 0.0f;

        /// <summary>
        /// Makes the Screen transition to hidden state.
        /// </summary>
        public override void Hide()
        {
            toHidden = true;

            base.Hide();
        }

        #endregion

        #region Initialization

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screenManager">The ScreenManager that this Screen is associated to.</param>
        public MainMenuScreen(ScreenManager screenManager)
            : base(screenManager)
        {
            Name = "MainMenuScreen";
        }

        /// <summary>
        /// Called when the Screen needs to be initialized.
        /// </summary>
        public override void Initialize()
        {
            gameScore = Game.Services.GetService(typeof(GameScore)) as GameScore;
            if (gameScore == null)
                throw new InvalidOperationException("Service of type GameScores not found");

            Components3D.Add(new MenuBackground(this) { Duration = TimeSpan.FromSeconds(6) });

            ApplicationMenu menu = new ApplicationMenu(this);
            // menu title
            menu.MenuTitle = "TITLE_MAIN_MENU";
            // menu items
            List<ApplicationMenu.IMenuItem> auxMenuItems = new List<ApplicationMenu.IMenuItem>();
            auxMenuItems.Add(new ApplicationMenu.LinkItem("ITEM_PLAY", new ApplicationMenu.ButtonMessage[] { ApplicationMenu.ButtonMessage.SELECT, ApplicationMenu.ButtonMessage.BACK }));
            auxMenuItems.Add(new ApplicationMenu.LinkItem("ITEM_OPTIONS", new ApplicationMenu.ButtonMessage[] { ApplicationMenu.ButtonMessage.SELECT, ApplicationMenu.ButtonMessage.BACK }));
            auxMenuItems.Add(new ApplicationMenu.LinkItem("ITEM_HIGHSCORES", new ApplicationMenu.ButtonMessage[] { ApplicationMenu.ButtonMessage.SELECT, ApplicationMenu.ButtonMessage.BACK }));
            auxMenuItems.Add(new ApplicationMenu.LinkItem("ITEM_EXIT", new ApplicationMenu.ButtonMessage[] { ApplicationMenu.ButtonMessage.SELECT, ApplicationMenu.ButtonMessage.BACK }));
            menu.MenuItems = auxMenuItems;
            // menu actions
            menu.MenuAccept += (sender, args) =>
                {
                    #region Menu Actions

                    switch (args.Index)
                    {
                        case 0:
                            // play
                            Load_GamePlayScreen();
                            break;
                        case 1:
                            // options
                            ScreenManager.AddScreen(new OptionsMenuScreen(ScreenManager));
                            break;
                        case 2:
                            // highscores
                            ScreenManager.AddScreen(new HighScoresScreen(ScreenManager));
                            break;
                        case 3:
                            // exit
                            ScreenManager.AddScreen(new ExitScreen(ScreenManager));
                            break;
                        default:
                            break;
                    }

                    #endregion
                };

            Components2D.Add(menu);

            base.Initialize();
        }

        /// <summary>
        /// Loads gameplay screen.
        /// </summary>
        private void Load_GamePlayScreen()
        {
            gameScore.Lives = 3;
            gameScore.Score = 0;
            gameScore.LevelPosition = 0.0f;
            gameScore.PlayTime = TimeSpan.Zero;

            toPlay = true;

            LoadingScreen.Load(ScreenManager, new IntroScreen(ScreenManager), LoadTime.FAST,
                               (contentManager, assetIndex) =>
                               {
                                   #region Asset Loading

#if DEBUG
                                   if (assetIndex == 33)
                                       return true;
#else
                                   if (assetIndex == 31)
                                       return true;
#endif

                                   switch (assetIndex)
                                   {
                                       case 0:
                                           contentManager.Load<SpriteSheet>(ContentSettings.GetAsset("HUD_SPRITES"));
                                           break;
                                       case 1:
                                           contentManager.Load<Texture2D>(ContentSettings.GetAsset("BROKEN_GLASS_01"));
                                           break;
                                       case 2:
                                           contentManager.Load<Texture2D>(ContentSettings.GetAsset("BROKEN_GLASS_02"));
                                           break;
                                       case 3:
                                           contentManager.Load<Texture2D>(ContentSettings.GetAsset("BROKEN_GLASS_03"));
                                           break;
                                       case 4:
                                           contentManager.Load<Texture2D>(ContentSettings.GetAsset("GLOW_EFFECT"));
                                           break;
                                       case 5:
                                           contentManager.Load<SpriteSheet>(ContentSettings.GetAsset("MSG_SPRITES"));
                                           break;
                                       case 6:
                                           contentManager.Load<Effect>(ContentSettings.GetAsset("SKYBOX_EFFECT"));
                                           break;
                                       case 7:
                                           contentManager.Load<TextureCube>(ContentSettings.GetAsset("SKYBOX_TEXTURE"));
                                           break;
                                       case 8:
                                           contentManager.Load<SpriteSheet>(ContentSettings.GetAsset("LENSFLARE_SPRITES"));
                                           break;
                                       case 9:
                                           contentManager.Load<Effect>(ContentSettings.GetAsset("PARTICLES_EFFECT"));
                                           break;
                                       case 10:
                                           contentManager.Load<Texture2D>(ContentSettings.GetAsset("PARTICLES_TEXTURE"));
                                           break;
                                       case 11:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("ASTEROID_01_HIGH"));
                                           break;
                                       case 12:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("ASTEROID_02_HIGH"));
                                           break;
                                       case 13:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("ASTEROID_03_HIGH"));
                                           break;
                                       case 14:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("ASTEROID_01_LOW"));
                                           break;
                                       case 15:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("ASTEROID_02_LOW"));
                                           break;
                                       case 16:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("ASTEROID_03_LOW"));
                                           break;
                                       case 17:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("SMALL_ASTEROID_01"));
                                           break;
                                       case 18:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("SMALL_ASTEROID_02"));
                                           break;
                                       case 19:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("SMALL_ASTEROID_03"));
                                           break;
                                       case 20:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("BOMB_HIGH"));
                                           break;
                                       case 21:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("BOOST_HIGH"));
                                           break;
                                       case 22:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("FUEL_HIGH"));
                                           break;
                                       case 23:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("HEALTH_HIGH"));
                                           break;
                                       case 24:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("LIFE_HIGH"));
                                           break;
                                       case 25:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("BOMB_LOW"));
                                           break;
                                       case 26:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("BOOST_LOW"));
                                           break;
                                       case 27:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("FUEL_LOW"));
                                           break;
                                       case 28:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("HEALTH_LOW"));
                                           break;
                                       case 29:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("LIFE_LOW"));
                                           break;
                                       case 30:
                                           contentManager.Load<Texture2D>(ContentSettings.GetAsset("POWER_UP_GLOW"));
                                           break;
#if DEBUG
                                       case 31:
                                           contentManager.Load<SpriteFont>(ContentSettings.GetAsset("FONT_TEST"));
                                           break;
                                       case 32:
                                           contentManager.Load<Model>(ContentSettings.GetAsset("BOUNDING_SPHERE"));
                                           break;
#endif
                                       default:
                                           break;
                                   }

                                   return false;

                                   #endregion
                               });
        }

        #endregion

        #region Update

        /// <summary>
        /// Called when the Screen needs to be updated.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        public override void Update(GameTime gameTime)
        {
            #region Screen Transition

            if (toHidden)
            {
                // update transition timer
                float trDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / Time.TotalMilliseconds);
                trTime += trDelta;
                trTime = MathHelper.Clamp(trTime, 0.0f, 1.0f);
                if (trTime == 1.0f)
                { 
                    toHidden = false; trTime = 0.0f;
                    if (toPlay) { PlayList.Stop(); ScreenSound.LoadGame(); }
                }
            }

            #endregion

            base.Update(gameTime);
        }

        #endregion
    }
}
